Skip to content

Conversation

oliveror
Copy link
Member

Changes

Defines when a mounted page refreshes its data: when the user comes back to it and the data is stale.

  • useRecipeQuery listens to window focus and document visibilitychange. If the shown data is older than RECIPE_FRESHNESS_MS (30 s, the same as the read cache), it drops that cache entry and refetches.
  • The refresh uses the existing background-refresh path: Explore, My Cookbook and recipe detail keep showing the current data until the new data arrives (no blank grid); a failed refresh shows the existing "Could not refresh" message with Retry.
  • Data served from the cache keeps its real age, so a cached page is not refetched needlessly.
  • No polling or timers: nothing is fetched while the page is in the background.
  • The recipe editor opts out (refreshWhenStale: false), so typed input is never replaced.

Validation

  • npm run check passes (215 unit tests), incl. new tests: no refetch on focus after 10 s; background refetch after 31 s with the old data kept until the new arrives; no refetch when refreshWhenStale is off.

AI usage

Written with Claude Code (Anthropic) to address a teammate's review of the final-delivery findings. Please review the diff as you would any other contribution.

Closes #107

Recipe reads were cached for 30 s, but a page left open never refreshed.
useRecipeQuery now refetches when the window regains focus or the tab
becomes visible, if the shown data is older than RECIPE_FRESHNESS_MS
(30 s). It reuses the background-refresh path, so the grid stays visible,
and the recipe editor opts out (refreshWhenStale: false) so typed input
is never replaced.

Closes #107
@oliveror oliveror requested a review from a team as a code owner November 16, 2026 18:14
Copy link
Member

@olestan olestan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Request changes:

  • apps/web/src/features/recipes/useRecipeQuery.ts:100–103: fetchedAt.current is updated before the refresh succeeds. If the request fails, the old data remains visible but is treated as fresh for another 30 seconds, preventing a refetch when the user returns during that period.
  • Consider updating the timestamp only after a successful fetch and using a separate in-flight guard to prevent duplicate requests.
  • A regression test covering a failed refresh followed by another focus event would help ensure stale data is retried correctly.

fetchedAt was set when a background refresh started, so a failed refresh
left old data marked fresh for another 30 seconds. It is now only set when
a request succeeds, and a separate in-flight flag stops focus and
visibilitychange from starting the same refresh twice. Test: failed refresh,
then focus again, retries immediately.
@oliveror
Copy link
Member Author

Thanks, good catch. Fixed in the latest commit:

  • Timestamp only on success: fetchedAt is now set only when a request succeeds. A failed refresh leaves the data stale, so the next focus or visibilitychange retries straight away.
  • Separate in-flight guard: a refreshInFlight ref is set when a freshness refresh starts and cleared when its request settles (success or error). It stops focus and visibilitychange, which often fire together, from starting the same refresh twice.
  • Regression test: the data goes stale and both events fire at once (one request), then focus again (still one). The refresh fails: the old data stays and refreshError is set. Focus 1 s later retries and gets fresh data, which clears the error. This test fails on the previous commit.

npm run check (216 unit tests) passes.

…eshness

# Conflicts:
#	apps/web/src/features/recipes/RecipeCreatePage.tsx
@oliveror oliveror merged commit 79a588a into dev Nov 17, 2026
2 checks passed
@oliveror oliveror deleted the feature/recipe-data-freshness branch November 20, 2026 15:31
Sign in to join this conversation on GitHub.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants